package 实现无头单链表;

public class MySingleLinkedList implements SingleLinkedList{
    static class ListNode{
        public int data;
        public ListNode next;
        public ListNode(int data){
            this.data = data;
        }
    }
    ListNode head;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head==null){
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if (head==null){
            this.head = listNode;
        }else {
            ListNode cur = this.head;
            while (cur.next != null){
                cur = cur.next;
            }
            cur.next = listNode;
        }
    }

    @Override
    public boolean addIndex(int index, int data) {
        if (index < 0 || index>size()){
            return false;
        }
        ListNode listNode = new ListNode(data);
        if (index == 0){
            addFirst(data);
            return true;
        }
        if (index == size()){
            addLast(data);
            return true;
        }
        ListNode cur = head;
        int n = 1;
        while (cur.next!=null && n<index){
            cur = cur.next;
            n++;
        }
        listNode.next = cur.next;
        cur.next = listNode;
        return true;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur!=null){
            if (cur.data==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (this.head == null){
            return;
        }
        if (this.head.data == key){
            this.head=this.head.next;
            return;
        }
        ListNode cur = this.head;
        while (cur.next!=null){
            if (cur.next.data == key){
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(this.head==null){
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur!=null){
            if (cur.data == key){
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }
        if (this.head.data==key){
            this.head = this.head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur!=null){
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head = null;
    }
}
